package cnn.user.controller;

import cnn.sys.RestSession;
import cnn.user.entity.UserDTO;
import cnn.user.entity.UserQTO;
import cnn.user.service.SalaryService;
import cnn.user.service.UserService;
import cnn.topfox.annotation.TokenOff;
import cnn.user.entity.SalaryQTO;
import cnn.user.entity.UserLoginQTO;
import cnn.topfox.common.CommonException;
import cnn.topfox.common.Response;
import cnn.topfox.common.ResponseCode;
import cnn.topfox.misc.Misc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    SalaryService salaryService;

    @Autowired RedisTemplate redisTemplate;


    @GetMapping("/redis")
    public void redis(UserQTO qto) {
        salaryService.list(new SalaryQTO());
        Map<String,Object> mapData=redisTemplate.opsForHash().entries("com.user.entity.UserDTO:1");
        log.warn("{}", mapData.size()>3?("有"+mapData.size()):"无--------------");
        userService.getObject(2,false);
    }


    @TokenOff
    @PostMapping
    public Response<UserDTO> insert(@RequestBody UserDTO dto) {
        userService.insert(dto);
        return new Response<>(dto, 1);
    }

    @TokenOff
    @PutMapping
    public Response<UserDTO> update(@RequestBody UserDTO dto) {
//        Misc.checkObjNotNull(dto, "id");
        int count = userService.update(dto);
        return new Response<>(dto, count);
    }

    @TokenOff
    @DeleteMapping("/{ids}")
    public Response<Integer> delete(@PathVariable("ids") String ids) {
        Misc.checkObjNotNull(ids, "ids");
        int count = userService.deleteByIds(ids);
        return new Response<>(count);
    }

    /**
     * 一次提交,存在 增加 修改 删除的情况
     *
     * @return
     */
    @TokenOff
    @PostMapping("/save")
    public Response<List<UserDTO>> save(@RequestBody List<UserDTO> list) {
        userService.save(list);
        return new Response<List<UserDTO>>(list);
    }

    @TokenOff
    @GetMapping
    public Response<List<UserDTO>> list(UserQTO qto) {
        //qto.setPageSize(21);
        //优先执行 mapper.xml 对应的SQL sqlId=list, 没有则类库自动生成当前表所有字段的查询SQL

        return userService.list(qto);
    }

    @TokenOff
    @GetMapping("/{id}")
    public Response<UserDTO> get(@PathVariable("id") String id) {
        Misc.checkObjNotNull(id, "id");

        UserQTO user = new UserQTO();
        user.setId(id);
        List<UserDTO> list = userService.list(user).data();
        return new Response(list.size() > 0 ? list.get(0) : null);

        /**
         * 说明: 以上的方式是, sql语句可以自定义,返回的字段可以比当前表的字段多
         * 如果只返回本表字段,可以
         * return userService.getObject(id);
         */
    }

    @TokenOff
    @GetMapping("/exception")
    public void newCommonException(UserQTO qto) {
        //异常抛出禁止 使用 + 串联字符串, 应用 以下方式避免这个问题; 类库还封装了分布式下的需要的一些信息
        throw CommonException.newInstance(ResponseCode.DATA_IS_NULL)
                .text("a", " and ", " b", " c");
    }

    @TokenOff
    @GetMapping(value = "/login", name = "登录")
    public Response<RestSession> login(UserLoginQTO userQTO) {
        Misc.checkBeansNotNull(userQTO, "userId", "password");
        Response<RestSession> response = userService.login(userQTO);
        return response;
    }

}